package test.collector;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.quartz.Scheduler;
import org.quartz.SchedulerMetaData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import collector.core.ConfigTask;
import collector.core.TaskConstVO;
import collector.core.config.ConfigModule;
import collector.core.util.JobTaskUtil;
import collector.core.util.ScheduleUtil;
import collector.task.JobTaskVO;

/**
 * <pre>
 * Class : collector.cimeCollector / cimeCollector.java
 * 	Define Class Object : collector의 기능 테스트를 위한 일회성 실행 프로그램 
 * <br>
 * </pre>
 * @author ytjeong / apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2012. 9. 21.
 */
public class cimeCollector {

	private static Logger _log = LoggerFactory.getLogger(cimeCollector.class);
	private static final String _LogPrefixStr = "col Log> ";
	List<JobTaskVO> jobTaskList = new ArrayList<JobTaskVO>();
	Map<String, JobTaskVO> jobTaskMap = new HashMap<String, JobTaskVO>();
	
	@SuppressWarnings("unused")
	private ConfigModule configModule = ConfigModule.getConfig(ConfigModule.class);

	//private Config config = Config.newInstance();
	
	public void runScheduler() throws Exception {
	    
		_log.info(_LogPrefixStr + "| " + "----- Initializing -------------------");
	    	ConfigTask configTask = new ConfigTask();
	    	configTask.loadConfigTask(); 
	    	 _log.debug(_LogPrefixStr + "| " + " configTask="+configTask);
	    	
	    
		Scheduler sched = ScheduleUtil.sf.getScheduler();
		_log.info(_LogPrefixStr + "| " + "----- Initialization Complete --------");

	        _log.info(_LogPrefixStr + "| " + "----- Scheduling Jobs ----------------");
	        JobTaskVO jobTaskVo = null;
	        TaskConstVO tConvVo = null;
	        
//	        // --------------- Job1/1
//	        jobTaskVo = JobTaskUtil.generateJobTask("jobName1", "jobGroup1",  HelloJob.class, "triggerName1", "triggerGroup1", "034200", "MINUTE", 1);
//	        sched = ScheduleUtil.scheduleJobTask(sched, jobTaskVo);
//	        jobTaskList.add(jobTaskVo);
	        Map<String, TaskConstVO> conVoMap = configTask.getConVoMap();
	        
	        // Case for Map Type
	        @SuppressWarnings("rawtypes")
		Iterator itr = conVoMap.keySet().iterator();
	        while (itr.hasNext()) {
	            String keyTaskName = (String)itr.next();
	            tConvVo = conVoMap.get(keyTaskName);
	            	
	            jobTaskVo = JobTaskUtil.generateJobTask(keyTaskName, tConvVo.getTaskGroup(), 
	        	    						tConvVo.getTaskClassStr(), 
	        	    						keyTaskName+"_Trigger", keyTaskName+"_TriggerGroup", 
	        	    						tConvVo.getScheduleDateStr(), tConvVo.getDurationType(), 
	        	    						tConvVo.getDurationValue());

	            sched = ScheduleUtil.scheduleJobTask(sched, jobTaskVo);

	            // 스케줄 관련 작업 정보 출력
	            String  jobTaskScheduleMsg = jobTaskVo.toStringInfoSchedule(); 
	            String taskConstMsg = tConvVo.viewJobSchedule(); 
	            _log.info(_LogPrefixStr + "| " + "----- Scheduled Jobs ---------------- ---------------- ---------------- ----------------");
	            _log.info(_LogPrefixStr + "| " + "----- Scheduled Jobs ----------------" + "tConvVo | " + taskConstMsg);
	            _log.info(_LogPrefixStr + "| " + "----- Scheduled Jobs ----------------" + "jobTaskVo | " + jobTaskScheduleMsg);
	            _log.info(_LogPrefixStr + "| " + "----- Scheduled Jobs ---------------- ---------------- ---------------- ----------------");
	            
	            jobTaskList.add(jobTaskVo);
	            jobTaskMap.put(jobTaskVo.getJobIDs(), jobTaskVo);
	            
	        }
	        // Case for List Type
//	        for ( TaskConstVO tConvVo : configTask.getConVoMap() ) {
//	            
//		        jobTaskVo = JobTaskUtil.generateJobTask(tConvVo.getTaskName(), tConvVo.getTaskGroup(),  (Class<? extends Job>) tConvVo.getTaskClass(), tConvVo.getTaskName()+"_Trigger", tConvVo.getTaskName()+"_TriggerGroup", tConvVo.getScheduleDateStr(), tConvVo.getDurationType(), tConvVo.getDurationValue());
//		        sched = ScheduleUtil.scheduleJobTask(sched, jobTaskVo);
//		        jobTaskList.add(jobTaskVo);
//	        }
	        _log.debug(_LogPrefixStr + "| " + jobTaskList.size() + " rec jobTaskList="+jobTaskList);
	        
	        _log.info(_LogPrefixStr + "| " + "----- Starting Scheduler ----------------");
	        // All of the jobs have been added to the scheduler, but none of the jobs
	        // will run until the scheduler has been started
	        sched.start();
	        _log.info(_LogPrefixStr + "| " + "----- Started Scheduler -----------------");

	        _log.info(_LogPrefixStr + "| " + "----- Waiting 600 seconds... -------------");
	        try {
	            // wait five minutes to show jobs
	            Thread.sleep(10 * 600L * 1000L); 
	            // executing...
	        } catch (Exception e) {
	        }

	        _log.info(_LogPrefixStr + "| " + "----- Shutting Down ---------------------");
	        sched.shutdown(true);
	        _log.info(_LogPrefixStr + "| " + "----- Shutdown Complete -----------------");

	        SchedulerMetaData metaData = sched.getMetaData();
	        _log.info(_LogPrefixStr + "| " + "Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
	        
	}

	    public static void main(String[] args) throws Exception {

		cimeCollector example = new cimeCollector();
	        //example.runState();
	        example.runScheduler();
	        
	    }
}
